

import { Utils } from '../utils';
import { ValidatorFn } from './form.interface';


/**
 * @description 动态表单 验证器
 */
export class Validators {
	/**
	 * @description 非空
	 */
	public static required: ValidatorFn = (value: any) => {
		const valid = Utils.isNotEmpty(value);
		return {
			valid,
			type: 'required',
		};
	};

	/**
	 * @description 手机
	 */
	public static mobile: ValidatorFn = (value: string) => {
		const reg = /^1[3-9]\d{9}$/;
		const valid = reg.test(value);
		if (Utils.isEmpty(value)) {
			return {
				valid: true,
				type: 'mobile',
			};
		}
		return {
			valid,
			type: 'mobile',
		};
	}

	/**
	 * @description 电子邮箱
	 */
	public static email: ValidatorFn = (value: string) => {
		const reg = /^[a-z|A-Z|0-9]{2,}@[a-z|A-Z|0-9]{2,}/;
		const valid = reg.test(value);
		if (Utils.isEmpty(value)) {
			return {
				valid: true,
				type: 'email',
			};
		}
		return {
			valid,
			type: 'email',
		};
	}

	/**
	 * @description 身份证
	 */
	public static idcard: ValidatorFn = (value: string) => {
		const reg = /^[1-9]\d{16}([0-9]|X|x)$/;
		const valid = reg.test(value);
		if (Utils.isEmpty(value)) {
			return {
				valid: true,
				type: 'idcard',
			};
		}
		return {
			valid,
			type: 'idcard',
		};
	}

	/**
	 * @description 必须为 true
	 */
	public static true: ValidatorFn = (value) => {
		const valid = !!value;
		return {
			valid,
			type: 'true',
		};
	}

	/**
	 * @description 最小值
	 */
	public static min = (min: number): ValidatorFn => {
		return (value) => {
			if (Utils.isEmpty(value)) {
				return {
					valid: true,
					type: 'minNumber',
					data: { min },
				};
			}
			const valid = Utils.isNumber(+value) && +value >= min;
			return {
				valid,
				type: 'minNumber',
				data: {min},
			};
		};
	}
	/** 最大值 */
	public static max = (max: number): ValidatorFn => {
		return (value) => {
			if (Utils.isEmpty(value)) {
				return {
					valid: true,
					type: 'maxNumber',
					data: { max },
				};
			}
			const valid = Utils.isNumber(+value) && +value <= max;
			return {
				valid,
				type: 'maxNumber',
				data: {max},
			};
		};
	};
	/** @description 最大长度 */
	public static maxLength = (max: number): ValidatorFn => {
		return (value) => {
			if (Utils.isEmpty(value)) {
				return {
					valid: true,
					type: 'maxLength',
					data: { max },
				};
			}
			const strValue = value + '';
			const valid = strValue.length <= max;
			return {
				valid,
				type: 'maxLength',
				data: { max },
			};
		};
	}
	/** @description 最小长度 */
	public static minLength = (min: number): ValidatorFn => {
		return (value) => {
			if (Utils.isEmpty(value)) {
				return {
					valid: true,
					type: 'minLength',
					data: { min },
				};
			}
			const strValue = value + '';
			const valid = strValue.length >= min;
			return {
				valid,
				type: 'minLength',
				data: { min },
			};
		};
	}
	/** @description 固定长度 */
	public static fixLength = (length: number): ValidatorFn => {
		return (value) => {
			if (Utils.isEmpty(value)) {
				return {
					valid: true,
					type: 'fixLength',
					data: { length },
				};
			}
			const strValue = value + '';
			const valid = strValue.length === length;
			return {
				valid,
				type: 'fixLength',
				data: {
					length,
				}
			};
		};
	}

	/** 正则验证 */
	public static reg = (strReg: string, errorMsg: string): ValidatorFn => {
		const reg = new RegExp(strReg);
		return (value) => {
			if (Utils.isEmpty(value)) {
				return {
					valid: true,
					type: 'reg',
					data:{errorMsg,},
				};
			}
			const valid = reg.test(value+'');
			return {
				valid,
				type: 'reg',
				data:{errorMsg,},
			}
		}
	}

}
